<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="微信订单号" prop="transactionId">
        <el-input
          v-model="queryParams.transactionId"
          placeholder="请输入微信订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商户订单号" prop="outTradeNo">
        <el-input
          v-model="queryParams.outTradeNo"
          placeholder="请输入商户订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商户退款单号" prop="outRefundNo">
        <el-input
          v-model="queryParams.outRefundNo"
          placeholder="请输入商户退款单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="微信退款单号" prop="refundId">
        <el-input
          v-model="queryParams.refundId"
          placeholder="请输入微信退款单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退款金额" prop="refundFee">
        <el-input
          v-model="queryParams.refundFee"
          placeholder="请输入退款金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="应结退款" prop="settlementRefundFee">
        <el-input
          v-model="queryParams.settlementRefundFee"
          placeholder="请输入应结退款"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="标价金额" prop="totalFee">
        <el-input
          v-model="queryParams.totalFee"
          placeholder="请输入标价金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="应结订单金额" prop="settlementTotalFee">
        <el-input
          v-model="queryParams.settlementTotalFee"
          placeholder="请输入应结订单金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="现金支付金额" prop="cashFee">
        <el-input
          v-model="queryParams.cashFee"
          placeholder="请输入现金支付金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退款金额（分）" prop="cashRefundFee">
        <el-input
          v-model="queryParams.cashRefundFee"
          placeholder="请输入退款金额（分）"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="营销详情" prop="promotionDetail">
        <el-input
          v-model="queryParams.promotionDetail"
          placeholder="请输入营销详情"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="返回状态码" prop="returnCode">
        <el-input
          v-model="queryParams.returnCode"
          placeholder="请输入返回状态码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="返回信息" prop="returnMsg">
        <el-input
          v-model="queryParams.returnMsg"
          placeholder="请输入返回信息"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="业务结果" prop="resultCode">
        <el-input
          v-model="queryParams.resultCode"
          placeholder="请输入业务结果"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="错误代码" prop="errCode">
        <el-input
          v-model="queryParams.errCode"
          placeholder="请输入错误代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="错误代码" prop="errCodeDes">
        <el-input
          v-model="queryParams.errCodeDes"
          placeholder="请输入错误代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="错误代码" prop="errorCode">
        <el-input
          v-model="queryParams.errorCode"
          placeholder="请输入错误代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="错误代码" prop="errorMessage">
        <el-input
          v-model="queryParams.errorMessage"
          placeholder="请输入错误代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="公众账号ID" prop="appid">
        <el-input
          v-model="queryParams.appid"
          placeholder="请输入公众账号ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商户号" prop="mchId">
        <el-input
          v-model="queryParams.mchId"
          placeholder="请输入商户号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="随机字符串" prop="nonceStr">
        <el-input
          v-model="queryParams.nonceStr"
          placeholder="请输入随机字符串"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="签名" prop="sign">
        <el-input
          v-model="queryParams.sign"
          placeholder="请输入签名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="回调" prop="isNotify">
        <el-input
          v-model="queryParams.isNotify"
          placeholder="请输入回调"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="回调时间" prop="notifyTime">
        <el-date-picker clearable
          v-model="queryParams.notifyTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择回调时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="回调备注" prop="notifyRemark">
        <el-input
          v-model="queryParams.notifyRemark"
          placeholder="请输入回调备注"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单id" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
     <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['accompany:refund:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['accompany:refund:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['accompany:refund:remove']"
        >删除</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['accompany:refund:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="refundList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="refundLogId" />
      <el-table-column label="微信订单号" align="center" prop="transactionId" />
      <el-table-column label="商户订单号" align="center" prop="outTradeNo" />
      <el-table-column label="商户退款单号" align="center" prop="outRefundNo" />
      <el-table-column label="微信退款单号" align="center" prop="refundId" />
      <el-table-column label="退款金额" align="center" prop="refundFee" />
      <el-table-column label="应结退款" align="center" prop="settlementRefundFee" />
      <el-table-column label="标价金额" align="center" prop="totalFee" />
      <el-table-column label="应结订单金额" align="center" prop="settlementTotalFee" />
      <el-table-column label="标价币种" align="center" prop="feeType" />
      <el-table-column label="现金支付金额" align="center" prop="cashFee" />
      <el-table-column label="现金支付币种" align="center" prop="cashFeeType" />
      <el-table-column label="退款金额（分）" align="center" prop="cashRefundFee" />
      <el-table-column label="营销详情" align="center" prop="promotionDetail" />
      <el-table-column label="返回状态码" align="center" prop="returnCode" />
      <el-table-column label="返回信息" align="center" prop="returnMsg" />
      <el-table-column label="业务结果" align="center" prop="resultCode" />
      <el-table-column label="错误代码" align="center" prop="errCode" />
      <el-table-column label="错误代码" align="center" prop="errCodeDes" />
      <el-table-column label="错误代码" align="center" prop="errorCode" />
      <el-table-column label="错误代码" align="center" prop="errorMessage" />
      <el-table-column label="公众账号ID" align="center" prop="appid" />
      <el-table-column label="商户号" align="center" prop="mchId" />
      <el-table-column label="随机字符串" align="center" prop="nonceStr" />
      <el-table-column label="签名" align="center" prop="sign" />
      <el-table-column label="回调" align="center" prop="isNotify" />
      <el-table-column label="回调时间" align="center" prop="notifyTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.notifyTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="回调备注" align="center" prop="notifyRemark" />
      <el-table-column label="订单id" align="center" prop="orderId" />
     <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['accompany:refund:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['accompany:refund:remove']"
          >删除</el-button>
        </template>
      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改微信退款日志对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="微信订单号" prop="transactionId">
          <el-input v-model="form.transactionId" placeholder="请输入微信订单号" />
        </el-form-item>
        <el-form-item label="商户订单号" prop="outTradeNo">
          <el-input v-model="form.outTradeNo" placeholder="请输入商户订单号" />
        </el-form-item>
        <el-form-item label="商户退款单号" prop="outRefundNo">
          <el-input v-model="form.outRefundNo" placeholder="请输入商户退款单号" />
        </el-form-item>
        <el-form-item label="微信退款单号" prop="refundId">
          <el-input v-model="form.refundId" placeholder="请输入微信退款单号" />
        </el-form-item>
        <el-form-item label="退款金额" prop="refundFee">
          <el-input v-model="form.refundFee" placeholder="请输入退款金额" />
        </el-form-item>
        <el-form-item label="应结退款" prop="settlementRefundFee">
          <el-input v-model="form.settlementRefundFee" placeholder="请输入应结退款" />
        </el-form-item>
        <el-form-item label="标价金额" prop="totalFee">
          <el-input v-model="form.totalFee" placeholder="请输入标价金额" />
        </el-form-item>
        <el-form-item label="应结订单金额" prop="settlementTotalFee">
          <el-input v-model="form.settlementTotalFee" placeholder="请输入应结订单金额" />
        </el-form-item>
        <el-form-item label="现金支付金额" prop="cashFee">
          <el-input v-model="form.cashFee" placeholder="请输入现金支付金额" />
        </el-form-item>
        <el-form-item label="退款金额（分）" prop="cashRefundFee">
          <el-input v-model="form.cashRefundFee" placeholder="请输入退款金额（分）" />
        </el-form-item>
        <el-form-item label="营销详情" prop="promotionDetail">
          <el-input v-model="form.promotionDetail" placeholder="请输入营销详情" />
        </el-form-item>
        <el-form-item label="返回状态码" prop="returnCode">
          <el-input v-model="form.returnCode" placeholder="请输入返回状态码" />
        </el-form-item>
        <el-form-item label="返回信息" prop="returnMsg">
          <el-input v-model="form.returnMsg" placeholder="请输入返回信息" />
        </el-form-item>
        <el-form-item label="业务结果" prop="resultCode">
          <el-input v-model="form.resultCode" placeholder="请输入业务结果" />
        </el-form-item>
        <el-form-item label="错误代码" prop="errCode">
          <el-input v-model="form.errCode" placeholder="请输入错误代码" />
        </el-form-item>
        <el-form-item label="错误代码" prop="errCodeDes">
          <el-input v-model="form.errCodeDes" placeholder="请输入错误代码" />
        </el-form-item>
        <el-form-item label="错误代码" prop="errorCode">
          <el-input v-model="form.errorCode" placeholder="请输入错误代码" />
        </el-form-item>
        <el-form-item label="错误代码" prop="errorMessage">
          <el-input v-model="form.errorMessage" placeholder="请输入错误代码" />
        </el-form-item>
        <el-form-item label="公众账号ID" prop="appid">
          <el-input v-model="form.appid" placeholder="请输入公众账号ID" />
        </el-form-item>
        <el-form-item label="商户号" prop="mchId">
          <el-input v-model="form.mchId" placeholder="请输入商户号" />
        </el-form-item>
        <el-form-item label="随机字符串" prop="nonceStr">
          <el-input v-model="form.nonceStr" placeholder="请输入随机字符串" />
        </el-form-item>
        <el-form-item label="签名" prop="sign">
          <el-input v-model="form.sign" placeholder="请输入签名" />
        </el-form-item>
        <el-form-item label="回调" prop="isNotify">
          <el-input v-model="form.isNotify" placeholder="请输入回调" />
        </el-form-item>
        <el-form-item label="回调时间" prop="notifyTime">
          <el-date-picker clearable
            v-model="form.notifyTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择回调时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="回调备注" prop="notifyRemark">
          <el-input v-model="form.notifyRemark" placeholder="请输入回调备注" />
        </el-form-item>
        <el-form-item label="订单id" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入订单id" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRefund, getRefund, delRefund, addRefund, updateRefund } from "@/api/accompany/refund";

export default {
  name: "Refund",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 微信退款日志表格数据
      refundList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        transactionId: null,
        outTradeNo: null,
        outRefundNo: null,
        refundId: null,
        refundFee: null,
        settlementRefundFee: null,
        totalFee: null,
        settlementTotalFee: null,
        feeType: null,
        cashFee: null,
        cashFeeType: null,
        cashRefundFee: null,
        couponRefundCount: null,
        couponRefundFee: null,
        promotionDetail: null,
        returnCode: null,
        returnMsg: null,
        resultCode: null,
        errCode: null,
        errCodeDes: null,
        errorCode: null,
        errorMessage: null,
        appid: null,
        mchId: null,
        nonceStr: null,
        sign: null,
        isNotify: null,
        notifyTime: null,
        notifyRemark: null,
        orderId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询微信退款日志列表 */
    getList() {
      this.loading = true;
      listRefund(this.queryParams).then(response => {
        this.refundList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        refundLogId: null,
        transactionId: null,
        outTradeNo: null,
        outRefundNo: null,
        refundId: null,
        refundFee: null,
        settlementRefundFee: null,
        totalFee: null,
        settlementTotalFee: null,
        feeType: null,
        cashFee: null,
        cashFeeType: null,
        cashRefundFee: null,
        couponRefundCount: null,
        couponRefundFee: null,
        promotionDetail: null,
        returnCode: null,
        returnMsg: null,
        resultCode: null,
        errCode: null,
        errCodeDes: null,
        errorCode: null,
        errorMessage: null,
        appid: null,
        mchId: null,
        nonceStr: null,
        sign: null,
        isNotify: null,
        notifyTime: null,
        notifyRemark: null,
        orderId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.refundLogId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加微信退款日志";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const refundLogId = row.refundLogId || this.ids
      getRefund(refundLogId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改微信退款日志";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.refundLogId != null) {
            updateRefund(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRefund(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const refundLogIds = row.refundLogId || this.ids;
      this.$modal.confirm('是否确认删除微信退款日志编号为"' + refundLogIds + '"的数据项？').then(function() {
        return delRefund(refundLogIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('accompany/refund/export', {
        ...this.queryParams
      }, `refund_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
